package com.bitsbazaar.manager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.bitsbazaar.dao.BaseDao;
import com.bitsbazaar.entity.User;

/**
 * 
 * @author Rachit
 *
 */
public class UserManagerImpl implements UserManager {
	
	
	
	private BaseDao dao;

	public BaseDao getDao() {
		return dao;
	}

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	/**
	 * Saves the object
	 * 
	 * @return the key(id) generated if success, -2 flag if email already exists, -1 flag for error
	 */
	@Override
	public Long registerUser(User user) {
		User alreadyExists = getUserByEmail(user.getEmail());
		if(alreadyExists == null)
			return dao.save(user);
		else
			return new Long(-2);
	}

	@Override
	/**
	 * Delete a User with the key id
	 * 
	 * @return true if success, false if failure
	 * @author Rachit
	 */
	public Boolean deleteUserById(Long id) {
		return dao.deleteById(User.class, id);
	}

	@Override
	/**
	 * Updates the User
	 * 
	 * @return true if success, false if failure/exception
	 * @author Rachit
	 */
	public Boolean updateUser(User user) {
		return dao.update(user);
		
	}

	@Override
	public List<User> getUserList() {
	return null;
	}
	
	/**
	 * Change password of the user by giving his id
	 * @return	true if success, false if not
	 * @author 	Rachit
	 */
	@Override
	public Boolean changePassword(Long Id,String newpassword, String oldpassword) {
		User user = getUserById(Id);
		if(user.getPassword().equals(oldpassword))
		{
			user.setPassword(newpassword);
			return updateUser(user);
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Authenticate the user by giving his id
	 * @return	true if authenticated, false if not
	 * @author 	Rachit
	 */
	@Override
	public Boolean userAuthenticateById(Long Id, String password){
		User user = getUserById(Id);
		if(user.getPassword().equals(password)){
			return true;
		}
		return false;
	}
	
	/**
	 * Authenticate the user by giving his email
	 * @return	true if authenticated, false if not
	 * @author 	Rachit
	 */
	@Override
	public Boolean userAuthenticateByEmail(String email, String password){
		User user = getUserByEmail(email);
		if((user!=null)&&user.getPassword().equals(password)){
			return true;
		}
		return false;
	}
	
	/**
	 * Authenticate the user by giving his email
	 * @return	true if authenticated, false if not
	 * @author 	Rachit
	 */
	@Override
	public Boolean adminAuthenticateByEmail(String email, String password){
		User user = getUserByEmail(email);
		if((user!=null)&&user.getPassword().equals(password)&&user.getType().equals("admin")){
			return true;
		}
		return false;
	}
	
	@Override
	public User getUserById(Long id) {
		return (User) dao.findById(User.class, id);
	}
	
	/**
	 * @return User if found, null otherwise
	 * @author Rachit
	 */
	@Override
	public User getUserByEmail(String email) {
		User user = null;
		List<Object> list = dao.findByProperty(User.class, "email", email);
		if(list.size()>0)	
			user = (User)list.get(0);	
		return user;
	}

}
